package top.shiyiri.web;

import top.shiyiri.pojo.Book;
import top.shiyiri.pojo.Page;
import top.shiyiri.service.BookService;
import top.shiyiri.service.impl.BookServiceImpl;
import top.shiyiri.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Aunean
 * @date 2022/2/8 11:23
 */
public class BookServlet extends BaseServlet {
    private BookService service = new BookServiceImpl();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取请求的参数封装成为Book对象
        Book book = WebUtils.copyParamsToBean(request.getParameterMap(), new Book());
        //2.调用service.addBook()保存图书
        service.addBook(book);
        //3.跳到图书列表页面 /manager/bookServlet?action=add
        //System.out.println(request.getContextPath());///book
        int pageNo = Integer.parseInt(request.getParameter("pageNo")) + 1;
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取请求的参数id
        String id = request.getParameter("id");
        //2.调用 service.deleteBookById() 删除图书
        service.deleteBookById(Integer.parseInt(id));
        //3.重定向会图书列表
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取请求的参数
        Book book = WebUtils.copyParamsToBean(request.getParameterMap(), new Book());
        //2.修改
        service.updateBook(book);
        //3.重定向
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求的参数【图书编号】
        String id = request.getParameter("id");
        //2.调用 service.queryBookById 查询图书信息
        Book book = service.queryBookById(Integer.parseInt(id));
        //3.保存图书信息到 Request 域中
        request.setAttribute("book", book);
        //4.请求转发到 /pages/manager/book_edit.jsp 页面
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request, response);
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过service.queryBooks()查询全部图书
        List<Book> books = service.queryBooks();
        //2.把全部图书保存到 Request 域中
        request.setAttribute("books", books);
        //3.请求转发到 /pages/manager/book_manager.jsp 页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }

    /**
     * 处理分页功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求的参数 pageNo 和 pageSize
        int pageNo = request.getParameter("pageNo") == null? 1 : (Integer.parseInt(request.getParameter("pageNo")));
        int pageSize = request.getParameter("pageSize") == null? Page.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
        //2.调用 service.page(pageNo, pageSize)：Page对象
        Page<Book> page = service.page(pageNo, pageSize);
        page.setUrl("manager/bookServlet?action=page");
        //3.保存page对象到Request域中
        request.setAttribute("page", page);
        //4.请求转发到 /pages/manager/book_manager.jsp 页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }
}

